<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import {useAuthStore} from "../../stores/authStore.js";
import {UserApi} from "@/api/user/index.js";

const router = useRouter()

// 登录表单数据
const loginForm = reactive({
  username: 'admin',
  password: 'Init@123'
})

// 错误信息
const errors = reactive({
  username: '',
  password: ''
})

// 状态变量
const loading = ref(false)
const showCaptcha = ref(false)
const captchaUrl = ref('https://picsum.photos/120/40')

// 表单验证
const validateForm = () => {
  let isValid = true
  return isValid
}

// 刷新验证码
const refreshCaptcha = async () => {
  captchaUrl.value = `https://picsum.photos/120/40?t=${Date.now()}`
}

const handleLogin = async () => {
  if (!validateForm()) return

  loading.value = true

  // 模拟登录API调用
  // 模拟登录成功后设置认证令牌
  const user = {
    username: loginForm.username,
    password: loginForm.password
  }
  /*localStorage.setItem('authToken', 'aaaa')*/
  const authStore = useAuthStore();
  await authStore.login(user);
  router.push('/');
}


// 跳转到注册页面
const goToRegister = () => {
  router.push('/register')
}

// 跳转到忘记密码页面
const goToForgotPassword = () => {
  router.push('/forgot-password')
}

// 跳转到手机号登录页面
const goToPhoneLogin = () => {
  router.push('/phonelogin')
}
</script>


<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h1>法律援助系统</h1>
        <p>为需要帮助的人提供法律支持</p>
      </div>

      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <label for="username">用户名/手机号</label>
          <input
              id="username"
              v-model="loginForm.username"
              type="text"
              placeholder="请输入用户名或手机号"
              required
          />
          <span class="error-message" v-if="errors.username">{{ errors.username }}</span>
        </div>

        <div class="form-group">
          <label for="password">密码</label>
          <input
              id="password"
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              required
          />
          <span class="error-message" v-if="errors.password">{{ errors.password }}</span>
        </div>

        <div class="form-group" v-if="showCaptcha">
          <label for="captcha">验证码</label>
          <div class="captcha-container">
            <input
                id="captcha"
                v-model="loginForm.captcha"
                type="text"
                placeholder="请输入验证码"
                required
            />
            <img
                :src="captchaUrl"
                class="captcha-img"
                @click="refreshCaptcha"
                alt="验证码"
            />
          </div>
        </div>

        <button type="submit" class="login-btn" :disabled="loading">
          {{ loading ? '登录中...' : '登 录' }}
        </button>

        <div class="login-footer">
          <a href="#" @click.prevent="goToRegister">注册账号</a>
          <a href="#" @click.prevent="goToForgotPassword">忘记密码</a>
          <a href="#" @click.prevent="goToPhoneLogin">手机号登录</a>
        </div>
      </form>
    </div>

    <div class="legal-notice">
      <p>本系统由法律援助中心提供，旨在为经济困难或特殊案件的当事人提供免费法律服务</p>
    </div>
  </div>
</template>


<style scoped>
.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background-color: #f5f7fa;
  background-image: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
  padding: 20px;
  font-family: 'Arial', sans-serif;
}

.login-card {
  width: 100%;
  max-width: 420px;
  padding: 40px 35px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  font-size: 24px;
  color: #333;
  margin: 15px 0 5px;
}

.login-header p {
  font-size: 14px;
  color: #999;
}

.logo {
  width: 80px;
  height: 80px;
}

.login-form {
  margin-top: 20px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #333;
}

.form-group input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  transition: border-color 0.3s;
}

.form-group input:focus {
  border-color: #409eff;
  outline: none;
}

.error-message {
  display: block;
  margin-top: 5px;
  color: #f56c6c;
  font-size: 12px;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background-color: #409eff;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.login-btn:hover {
  background-color: #66b1ff;
}

.login-btn:disabled {
  background-color: #a0cfff;
  cursor: not-allowed;
}

.login-footer {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
}

.login-footer a {
  color: #409eff;
  text-decoration: none;
  font-size: 14px;
}

.login-footer a:hover {
  text-decoration: underline;
}

.captcha-container {
  display: flex;
  align-items: center;
  gap: 10px;
}

.captcha-img {
  width: 120px;
  height: 40px;
  cursor: pointer;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.legal-notice {
  margin-top: 30px;
  text-align: center;
  font-size: 12px;
  color: #666;
  max-width: 420px;
}
</style>